    package controller;

    import pojo.Book;
    import pojo.Book;
    import service.BookService;
    import service.impl.BookServiceimpl;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    @WebServlet("/stu")
    public class bookServlet extends HttpServlet {
        List<Book> list = new ArrayList<Book>();
        BookService bookService = new BookServiceimpl();
        public void init() throws ServletException {
            this.getList();
        }

        public void getList(){
            bookService.getAllBook();

        }

        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doHandle(req, resp);
        }


        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doHandle(req, resp);
        }

        private void doHandle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");
            resp.setCharacterEncoding("UTF-8");
            String action = req.getParameter("action");
            if ("list".equals(action)) {
                this.getList();
                req.setAttribute("students", list);
                RequestDispatcher dispatcher = req.getRequestDispatcher("student.jsp");
                dispatcher.forward(req, resp);
            }

             else if (action.equals("add")) {
                String title = req.getParameter("title");
                String bookid = req.getParameter("bookid");
                String isbn = req.getParameter("isbn");
                String author = req.getParameter("author");
                String category = req.getParameter("category");

                Book book = new Book();
                book.setTitle(title);
                book.setBookid(Integer.parseInt(bookid));
                book.setIsbn(isbn);
                book.setAuthor(author);
                book.setCategory(category);

                bookService.addBook(book);

                RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
                dispatcher.forward(req, resp);

    //            try {
    //                //1.加载驱动
    //                Class.forName("com.mysql.cj.jdbc.Driver");
    //
    //                //2.创建数据库的链接  jdbc:mysql://hostname:port/databasename;
    //                String url = "jdbc:mysql://localhost:3306/my?serverTimezone=GMT%2B8";
    //                String user = "root";
    //                String pwd = "1234";
    //                Connection conn = DriverManager.getConnection(url, user, pwd);
    //
    //                //3.构造命令并执行命令
    //                String sql = "insert into students(stuNo,name,stuGender,stuGrade,stuDepart,stuMajor,stuClass)values(?,?,?,?,?,?,?)";
    //                PreparedStatement ps = conn.prepareStatement(sql);
    //                ps.setString(1,stuNo);
    //                ps.setString(2,name);
    //                ps.setString(3,gender);
    //                ps.setString(4,grade);
    //                ps.setString(5,depart);
    //                ps.setString(6,major);
    //                ps.setString(7,stuClass);
    //                ps.executeUpdate();
    //                conn.close();
    //                ps.close();
    //
    //                //返回界面
    //                RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
    //                dispatcher.forward(req, resp);
    //
    //            } catch (Exception e) {
    //                throw new RuntimeException(e);
    //            }

            }  else if (action.equals("delete")) {
                //获取要删除的学号
                String stuNo = req.getParameter("stuNo");
                bookService.deleteBook(bookid);
                RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
                dispatcher.forward(req, resp);
                //过滤不显示当前要删除的行

            }else if ("modify".equals(action)) {
                String stuNo = req.getParameter("stuNo");
                Book student = bookService.getBookid(stuNo);
                req.setAttribute("student", student);
                RequestDispatcher dispatcher = req.getRequestDispatcher("modifyStudent.jsp");
                dispatcher.forward(req, resp);



            }else if (action.equals("modify_sava")){
                String name = req.getParameter("name");
                String stuNo = req.getParameter("stuNo");
                String gender = req.getParameter("gender");
                String grade = req.getParameter("grade");
                String depart = req.getParameter("depart");
                String major = req.getParameter("major");
                String stuClass = req.getParameter("class");

                Book book = new Book();
                Book.s(name);
                book.setIsbn(stuNo);
                book.setTitle(gender);
                book.setAuthor(grade);
                book.setCategory(depart);

                bookService.updateStudent(book);
                RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
                dispatcher.forward(req, resp);

            }

        }
    }
